import dayjs from "dayjs";

export function dateFormat(row: any, column: any, value: any) {
  if (value) {
    return dayjs(value).format("YYYY-MM-DD");
  }
}

export function datetimeFormat(row: any, column: any, value: any) {
  if (value) {
    return dayjs(value).format("YYYY-MM-DD HH:mm:ss");
  }
}

export async function getImgToBase64(file: any) {
  const reader = new FileReader();
  reader.readAsDataURL(file.raw);
  reader.onload = () => {
    //此处便是得到的64位编码格式
    const image = reader.result;
    return image;
  };
}

export function cloneObject(target: any, source: any, ignore?: string[]) {
  if (!target || typeof target !== "object") {
    return target;
  }
  if (!source || typeof source !== "object") {
    return target;
  }
  for (const item in target) {
    if (ignore && ignore.length > 0) {
      if (!ignore.includes(item)) {
        if (
          Object.prototype.hasOwnProperty.call(target, item) &&
          Object.prototype.hasOwnProperty.call(source, item)
        ) {
          if (source[item] || target[item]) {
            target[item] = source[item];
          }
        }
      }
    } else {
      if (
        Object.prototype.hasOwnProperty.call(target, item) &&
        Object.prototype.hasOwnProperty.call(source, item)
      ) {
        if (source[item] || target[item]) {
          target[item] = source[item];
        }
      }
    }
  }
  return target;
}

export function cloneFromObject(target: any, source: any, ignore?: string[]) {
  if (!target || typeof target !== "object") {
    return target;
  }
  if (!source || typeof source !== "object") {
    return target;
  }
  for (const item in source) {
    if (ignore && ignore.length > 0) {
      if (!ignore.includes(item)) {
        if (
          Object.prototype.hasOwnProperty.call(target, item) &&
          Object.prototype.hasOwnProperty.call(source, item)
        ) {
          if (source[item] || target[item]) {
            target[item] = source[item];
          }
        }
      }
    } else {
      if (Object.prototype.hasOwnProperty.call(source, item)) {
        target[item] = source[item];
      }
    }
  }
  return target;
}

export function filterTreeNode(treeData: any, targetCode: any, nodeCode: any): any {
  if (!nodeCode) {
    nodeCode = "node_code";
  }
  for (let i = 0; i < treeData.length; i++) {
    const item = treeData[i];
    const itemCodeValue = item[nodeCode];
    if (itemCodeValue === targetCode || item.id === targetCode) {
      return item;
    } else {
      if (item.children && item.children.length > 0) {
        for (let j = 0; j < item.children.length; j++) {
          const childrenitem = item.children[j];
          const childrenItemValue = childrenitem[nodeCode];
          if (childrenItemValue === targetCode || childrenitem.id === targetCode) {
            return childrenitem;
          } else {
            if (childrenitem.children) {
              const matchItem = filterTreeNode(childrenitem.children, targetCode, nodeCode);
              if (matchItem) {
                return matchItem;
              }
            }
          }
        }
      }
    }
  }
}
